<template>
    <div
        v-for="(item, index) of list"
        :key="index"
        class="tree-item"
        :class="{
            close: !item.open,
            'no-child': !(item.children && item.children.length > 0)
        }"
    >
        <div class="button-box">
            <div class="line"></div>
            <div class="tree-row">
                <div class="button" v-if="item.children" @click="open(item)"></div>
                <div class="line-row" v-else></div>
            </div>
        </div>
        <div class="r">
            <div class="name-box">
                <div style="flex:1" class="tree-row">
                    <div
                        class="name"
                        @click="select(item)"
                        :class="item.id == curret1 ? 'active' : ''"
                    >{{ item.name }}</div>
                    <input
                        type="checkbox"
                        :checked="item.checked"
                        v-if="item.allowChecked"
                        @change="doCheck(item, !item.checked)"
                    />
                    <!-- <Checkbox
                        :checked="item.checked"
                    ></Checkbox>-->
                </div>
                <div class="tree-row select" v-if="item.id == curret1"></div>
            </div>
            <div v-if="item.children" class="children">
                <TreeRow
                    @select="(e) => doSelect(e, item)"
                    :list="item.children"
                    :curret1="curret1"
                />
            </div>
        </div>
    </div>
</template>
<script lang="ts">
import { defineComponent, ref, PropType } from "vue";
import { TreeItem } from './types';
import { Checkbox } from "ant-design-vue";
export default defineComponent({
    components: { Checkbox },
    name: "TreeRow",
    emits: {
        select: null,
        check: null
    },
    props: {
        list: {
            type: Array as PropType<TreeItem[]>,
            default: () => []
        },
        curret1: String,
    },
    setup(props: { list: TreeItem[] }, content) {
        function open(item: TreeItem) {
            item.open = !item.open
        }
        function select(item: TreeItem) {
            content.emit('select', [item])
        }
        function doSelect(e: TreeItem[], item: TreeItem) {
            content.emit('select', [item, ...e])
        }
        function doCheck(item: TreeItem, val: boolean) {
            function checkedFn(item: TreeItem, val: boolean) {
                if(item.allowChecked){
                    item.checked = val
                }
                if (item.children) {
                    item.children.forEach(x => checkedFn(x, val))
                }
            }
            checkedFn(item, val)
            content.emit('check')
        }

        return {
            open,
            select,
            doSelect,
            doCheck
        };
    },
});
</script> 
<style scoped lang="less">
@rowHeight: 20px;
@rowHeightHalf: 10px;

.tree-item {
    display: flex;
    white-space: nowrap;
    align-items: flex-start;
    white-space: nowrap;
    position: relative;
    padding-left: 16px;
    line-height: 1;
    .r {
        max-width: 100%;
        flex: 1;
        position: relative;
    }
    .name-box {
        display: flex;
        align-items: center;
    }
    .tree-row {
        position: relative;
        display: flex;
        align-items: center;
        height: @rowHeight;
        width: 100%;
        transition: height 100ms linear;
        overflow: hidden;
    }
    &:last-child {
        & > .button-box {
            .line {
                height: @rowHeightHalf;
            }
        }
    }

    .name {
        cursor: pointer;
        position: relative;
        z-index: 2;
        flex: 1;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
        &.active {
            color: maroon;
        }
    }
    .select {
        position: absolute;
        top: 0;
        width: 100vw;
        height: @rowHeight;
        z-index: -1;
        background: #e4eef9;
        transform: translateX(-50%);
    }
}
.button-box {
    position: absolute;
    overflow: hidden;
    width: 13px;
    top: 0;
    left: 0;
    height: 100%;
    .button {
        cursor: pointer;
        position: relative;
        z-index: 2;
        background-image: url(/static/img/zTreeStandard.png);
        background-repeat: no-repeat;
        background-position: -96px -41px;
        width: 9px;
        height: 9px;
        margin: 0 auto;
    }
    .line {
        z-index: 1;
        position: absolute;
        box-sizing: border-box;
        top: -1px;
        left: 6px;
        flex-shrink: 0;
        width: 1px;
        height: calc(100% + 1px);
        background: url(/static/img/dian1.png) 0 0 repeat-y;
    }

    .line-row {
        z-index: 1;
        position: absolute;
        top: 50%;
        left: 6px;
        width: 5px;
        border-top: 1px #ccc dotted;
    }
}
.no-child {
    & > .button-box {
        .line {
            height: 100%;
        }
    }
}
.close {
    & > .button-box {
        .button {
            background-position: -78px -41px;
        }
    }
    & .children {
        .tree-row {
            height: 0;
        }
    }
}
</style>
